﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;

public partial class controls_admin_Menu : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
            loadMenu();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
    }
    private void loadMenu()
    {
        try
        {
            base.OpenConnection();
            int m_id;
            if (Request["m_id"] != null)
                m_id = Convert.ToInt32(Request["m_id"]);
            else
                m_id = 0;
            DataTable objDT = BO_Sec_Admin_Modules.SelectParent(1, m_id);
            base.bindRepeater(rptData, objDT, divMessage);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            base.OpenConnection();
            Button btnID = (Button)e.Item.FindControl("btnID");
            int m_id = Convert.ToInt32(btnID.CommandArgument);
            string commandName = e.CommandName;
            if (commandName.Equals("del"))
            {
                BO_Sec_Admin_Modules.Delete(m_id);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
            }
            else if (commandName.Equals("edit"))
                Response.Redirect("Default.aspx?module=menuedit&m_id=" + m_id);
            //else if (commandName.Equals("DisHome"))
            //    BO_Categories.is_home(c_id);
            //else if (commandName.Equals("DisDetail"))
            //    BO_Categories.is_detail(c_id);
            else if (commandName.Equals("Up"))
                BO_Sec_Admin_Modules.Up(m_id);
            else if (commandName.Equals("Down"))
                BO_Sec_Admin_Modules.Down(m_id);
            loadMenu();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            Button btnID = (Button)e.Item.FindControl("btnID");
            int m_id = Convert.ToInt32(btnID.CommandArgument);
            int count = BO_Sec_Admin_Modules.SelectParent(1, m_id).Rows.Count;
            HtmlGenericControl divCountChild = (HtmlGenericControl)e.Item.FindControl("divCountChild");
            if (count > 0)
                divCountChild.InnerHtml = "<a  href='default.aspx?module=menu&m_id=" + m_id + "'>" + count + "</a>";
            else
            {
                divCountChild.InnerHtml = "0";
                HtmlAnchor aGoToChild = (HtmlAnchor)e.Item.FindControl("aGoToChild");
                aGoToChild.HRef = "";
                aGoToChild.Style.Add("Color", "Black");
            }
        }
        catch { }
    }
    protected void btnViewTrash_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx?module=viewnewstrash");
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx?module=menuadd");
    }
    protected void bnt_Order_Click(object sender, EventArgs e)
    {
        foreach(RepeaterItem item in rptData.Items)
        {
            Button control = (Button)item.FindControl("btnID");
            Response.Write(control.CommandArgument+"<br>");
        }
    }
}
